package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.constant.OrderRespConstants;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CancelOrderRequest;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * @ClassName ShoppingController
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/21 11:02
 **/

@RestController
@RequestMapping("shopping")
public class ShoppingController {

    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;


    @PostMapping("order")
    public ResponseData<String> order(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest request) {
        createOrderRequest.setUserId(getUid(request));
        String orderId = orderCoreService.createOrder(createOrderRequest);

        return new ResponseUtil<String>().setData(orderId);
    }

    @GetMapping("order")
    public ResponseData<OrderListResponse> allOrder(HttpServletRequest request, OrderListRequest orderListRequest) {
        // 获取用户id
        long uid = getUid(request);

        orderListRequest.setUserId(uid);

        OrderListResponse orderListResponse = orderCoreService.allOrder(orderListRequest);

        return new ResponseUtil<OrderListResponse>().setData(orderListResponse, "success");

    }

    private long getUid(HttpServletRequest request) {
        String userInfo = request.getHeader("user_info");

        JSONObject jsonObject = JSON.parseObject(userInfo);

        String uidStr = jsonObject.get("uid").toString();

        return Long.parseLong(uidStr);
    }

    @GetMapping("order/{id}")
    public ResponseData<OrderDetailResponse> orderDetail(@PathVariable("id") @NotNull String orderId) {

        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(orderId);

        return new ResponseUtil<OrderDetailResponse>().setData(orderDetailResponse, "success");
    }

    @PostMapping("cancelOrder")
    public ResponseData<String> cancelOrder(@RequestBody @NotNull CancelOrderRequest cancelOrderRequest) {
        orderCoreService.cancelOrder(cancelOrderRequest.getOrderId());

        return new ResponseUtil<String>().setData(OrderRespConstants.SUCESS_RESULT, OrderRespConstants.SUCCESS_MESSAGE);
    }


    @DeleteMapping("order/{id}")
    public ResponseData<String> deleteOrder(@PathVariable("id") @NotNull String id) {
        orderCoreService.deleteOrder(id);

        return new ResponseUtil<String>().setData(OrderRespConstants.SUCESS_RESULT, OrderRespConstants.SUCCESS_MESSAGE);

    }
}
